<?php

namespace App\Controllers\v1;

use App\Models\Money;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use common\utils\SysCode;
use App\Controllers\BaseController;
use App\Services\MoneyLogService;
use App\Services\OrderService;
use App\Services\UserService;
use App\Services\MoneyService;

class OrderController extends BaseController
{
    public $allows = ['orderMenu'];

    /**
     *交易记录-往来记录
     */
    public function recordFromsearch(Request $request)
    {
        $params = $request->all();
        $user = Auth::user();
        $result = MoneyLogService::getResult($user->id, $params);
        //获取钱包余额
        $result['balance'] = MoneyService::getWallet($user->id)['balance'];
        if ($result) {
            $result['list'] = MoneyLogService::formatMoneyLog($result['list']);
            //总存入金额
            $result['totalMoney']['income'] = MoneyLogService::getTotalIncomeMoney($user->id, $params);
            //总支出金额
            $result['totalMoney']['pay'] = MoneyLogService::getTotalPayMoney($user->id, $params);
        } else {
            $result['totalMoney']['income'] = $result['totalMoney']['pay'] = 0;
        }
        //转换单位
        $result = amount_format_array($result, ['balance', 'amount', 'coupon_amount', 'deal_money', 'income', 'pay', 'balance']);
        return $result;
    }

    /**
     *投注记录分类
     */
    public function orderMenu(Request $request)
    {
        $re = OrderService::getOrderMenu();
        return $re;
    }

    /**
     * 往来记录分类
     */
    public function recordMenu(Request $request)
    {
        return MoneyLogService::getRecordMenu();
    }

    /**
     *第三方游戏投注记录
     */
    public function recordGames(Request $request)
    {
        $user = Auth::user();
        $params = $request->all();
        $result = OrderService::getResult($user->id, $params);
        $totalMoney = OrderService::getTotalMoney($user->id, $params);
        $result['totalMoney'] = amount_format_array($totalMoney);
        $result['subTotalMoney'] = UserService::countSubTotal($result['list']);
        $result['list'] = UserService::formatOrders($result['list']);
        return $result;
    }

    /**
     * 有效投注查询
     * @param Request $request
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function getEffectiveBetting(Request $request)
    {
        $params = $this->validate($request, [
            'startTime' => 'required|date',
            'endTime' => 'required|date',
            'gameType' => 'required|Integer',
            'page' => 'Integer',
            'pageSize' => 'Integer',
        ]);

        $user = Auth::user();
        $ret = OrderService::getEffectiveBetting($user->id, $params);
        $ret["list"] = amount_format_array($ret["list"]);
        $ret["subTotalMoney"] = amount_format_array($ret["subTotalMoney"]);
        $ret["totalMoney"] = amount_format_array($ret["totalMoney"]);
        return $ret;
    }
}
